/* $Header: ramps.h,v 2.2 01/10/02 10:30:36 przybyls Exp $ */

/***********************************************************************/
/* INCLUDE FILE NAME:  ramps.h                                         */
/* ==================                                                  */
/*                                                                     */
/* Author:  Susan Merkel                                               */
/*                                                                     */
/* COMMENT:  Include file for ramp generator functions.                */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

#ifndef RAMPS_H
#define RAMPS_H

/* #include <typedefs.h> */


/*****************************/
/*   IFDEFS                  */
/*****************************/


/*****************************/
/*   SYMBOLIC CONSTANTS      */
/*****************************/


/* Define the maximum run length in milliseconds (999.99 minutes).     */

#define MAX_RUN_TIME         99999*600

/* Define the maximum run length in cmin         (999.99 minutes).     */

#define MAX_TIME_SETPT       99999

/* Ramp generator is scheduled at 20 millisecond intervals.            */

#define TIME_INTERVAL        20

/************************************************************************/
/*                                                                      */
/*  Notes on rate increments.                                           */
/*                                                                      */
/*                                                                      */
/*     setpt += (remainder + increment) DIV increment                   */
/*  remainder = (remainder + increment) MOD increment                   */
/*                                                                      */
/*                                                                      */
/* The ramp generator performs logical division with the rate increment */
/* Because of the, the increment must be a whole number.  A truncated   */
/* fraction will cause errors (rounding). If you end up with an         */
/* irrational number, you can apply a multiplier to the values before   */
/* stuffing into the ramp structures and use the same multiplier here.  */
/*                                                                      */
/* the increments units are the reciporcal of counts / tick.            */
/* rates are specified in units/min  (degrees K/min, ml/min, psi/min )  */
/* there are 50 ticks per second (ramp generator runs at 50 Hz)         */
/* and 60 seconds per minute (no suprise here)                          */
/*                                                                      */
/* thus one conversion is  3000 ticks/min                               */
/*                                                                      */
/* counts are the internal unit conversion from the specified units     */
/* for flows and pressure, this is 1 (since all of the rates and values */
/* are converted to internal units before being stuffed into the ramp   */
/* data structures). For temperatures, it is 64 since temps are         */
/* internally represented in 64ths degree K.                            */
/*                                                                      */
/* the routines that stuff                                              */
/*                                                                      */
/* the last conversion needed is the scaling used by the user interface */
/* to handle decimals.  for temps, the user interface multiplies the    */
/* real number by 100.  We need to do the same here.                    */
/*                                                                      */
/*  So, to get the increment:                                           */
/*                                                                      */
/*  multiplier * user interface scaling * ticks/min / iu scaling        */
/*                                                                      */
/*                                                                      */
/*                                                                      */
/************************************************************************/

/* Rate increment for oven and heated zones (468.75 x 100).            */

#define ZONE_RATE_INC     ( 10 * 100 * 3000 / 64 )  /*  46875 */

/* Rate increment for column, inlet, and aux pressure.                 */

#define PRES_RATE_INC     ( 1 * 1   * 3000 / 1  )  /*  3000 */

/*****************************/
/*   MACROS                  */
/*****************************/


/*****************************/
/*   TYPEDEFS                */
/*****************************/


/* These are the three "states" the ramp generator can be in, INITIAL  */
/* during the initial time period, RAMP while the zone is ramping, and */
/* FINAL during the final time period.                                 */

typedef enum { INITIAL, RAMP, FINAL, POST } RAMP_FLAG;

/* Each zone has an associated structure which is initialized prior    */
/* to calling the ramp generator.                                      */

typedef struct
{
  BIT8      ramp_enabled;  /* ramp only runs if TRUE!            */
  RAMP_TYPE ramp_type;     /* pres, flow, thermal */

  U_INT8    num_ramps;     /* number of ramps allowed            */
  BIT8      done;          /* True if ramp complete              */

  U_INT8    ramp_num;      /* which rate is currently ramping    */
  RAMP_FLAG ramp_state;    /* INITIAL, RAMP, FINAL, POST         */

  INT32     check_time;    /* ?                                  */

  U_INT32   remainder;     /* counts beyond setpt increment      */
  U_INT32   rate_increment;/* internal per tick setpt multiplier */

  RAMPS    *ramps;         /* rates, final values, final times   */
  RAMPTBL  *ramp_table;    /* init value, init time, post values */
  U_INT32  *setpoint;

} RAMPINFO, *pRAMPINFO;


/*****************************/
/*   EXTERNALS               */
/*****************************/


extern U_INT32   Run_Length;
extern U_INT32   Next_Run_Length;

extern RAMPINFO  Oven_Temp_Info;

extern RAMPINFO  Inja_Temp_Info;
extern RAMPINFO  Injb_Temp_Info;

extern RAMPINFO  Col1_Pres_Info;
extern RAMPINFO  Col2_Pres_Info;

extern RAMPINFO  Deta_Temp_Info;
extern RAMPINFO  Detb_Temp_Info;

extern RAMPINFO  Aux1_Temp_Info;
extern RAMPINFO  Aux2_Temp_Info;

extern RAMPINFO  Aux3_Pres_Info;
extern RAMPINFO  Aux4_Pres_Info;
extern RAMPINFO  Aux5_Pres_Info;


/*****************************/
/*   FUNCTION PROTOTYPES     */
/*****************************/

/* ramp_init.c */

void InitRamps(void);
void ResetRamps(void);
void SetThermalRampDefault( RAMPTBL *ptbl, RAMPS *pramp, INT32 setpt, U_INT32 levels );

/* ramps.c */

void SetRampLEDS(void);
void Ramps(void);

RAMP_TYPE GetRampType( pRAMPINFO rampinfo );
void SetRampType( pRAMPINFO rampinfo, RAMP_TYPE type );

RAMP_FLAG GetRampState( pRAMPINFO rampinfo );

void UpdateZoneInitValues(void);
void UpdatePneuRampInitValues( void );
void SetPresRampDefaults(void);
void CheckFinalTime(RAMPINFO *rampinfo);
void ProcessTime(RAMPINFO *rampinfo);
void CalculateRamp(RAMPINFO *rampinfo);
void DoPostRunValues(void);
void SetInitValues(void);

void CalcRunLength(void);

void SetRampSetptPointer( pRAMPINFO rampinfo, INT32 *ptr );
void DisableRamp( pRAMPINFO rampinfo );
void EnableRamp( pRAMPINFO rampinfo );


U_INT32 GetRampInitTime( pRAMPINFO rampinfo );
U_INT32 GetRampInitValue( pRAMPINFO rampinfo );
U_INT32 GetRampRate( pRAMPINFO rampinfo, U_INT8 ramp_num );
U_INT32 GetRampFinalTime( pRAMPINFO rampinfo, U_INT8 ramp_num );
U_INT32 GetRampFinalValue( pRAMPINFO rampinfo, U_INT8 ramp_num );
U_INT32 GetRampPostValue( pRAMPINFO rampinfo );

U_INT8 GetCurrentOvenRampNum( void );

BIT8 RampPostValueActive( void );
BIT8 RampInitialValueActive( pRAMPINFO rampinfo );

void SetRampInitTime( pRAMPINFO rampinfo, U_INT32 init_time );
void SetRampInitValue( pRAMPINFO rampinfo, U_INT32 init_value );
void SetRampRate( pRAMPINFO rampinfo, U_INT8 ramp_num, U_INT32 rate );
void SetRampFinalTime( pRAMPINFO rampinfo, U_INT8 ramp_num, U_INT32 final_time );
void SetRampFinalValue( pRAMPINFO rampinfo, U_INT8 ramp_num, U_INT32 final_value );
void SetRampPostValue( pRAMPINFO rampinfo, U_INT32 post_value );

pRAMPINFO pOvenRamp(void);
pRAMPINFO pInjaRamp(void);
pRAMPINFO pInjbRamp(void);
pRAMPINFO pDetaRamp(void);
pRAMPINFO pDetbRamp(void);
pRAMPINFO pAux1Ramp(void);
pRAMPINFO pAux2Ramp(void);
pRAMPINFO pCol1Ramp(void);
pRAMPINFO pCol2Ramp(void);
pRAMPINFO pAux3Ramp(void);
pRAMPINFO pAux4Ramp(void);
pRAMPINFO pAux5Ramp(void);


void SetPressureRampDefault( RAMPINFO *ramp_info );


#endif

